﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Collections;

namespace DBScript
{
    /// <summary>
    /// 数据库工厂
    /// </summary>
    public class DBFactory
    {

       

        #region GetDbConfig
        public static string GetConnectionString(string name)
        {
            DBScriptConfig config = ConfigurationManager.GetSection("DBScript") as DBScriptConfig;
            if (config.DbConfigs.ContainsKey(name))
            {
                return config.DbConfigs[name].ConnectionString;
            }
            return string.Empty;
        }
        #endregion

        /// <summary>
        /// 根据节点名为DBS的连接池,自动初始化
        /// </summary>
        /// <returns></returns>
        public static IDBBase GetDbProvider()
        {
            return GetDbProvider("DBScript");
        }

        #region GetDbProvider
        public static IDBBase GetDbProvider(string name)
        {
            string[] dbNames = name.Split(',');
            bool distributed = dbNames.Length>1?true:false;
            return GetDbProvider(name,distributed,true);
        }

        public static IDBBase GetDbProvider(string name, bool distributed, bool rw_separate)
        {
            DBScriptConfig config = ConfigurationManager.GetSection("DBScript") as DBScriptConfig;
            DBScriptConfig dsconfig = new DBScriptConfig();

            dsconfig.IsDebug = config.IsDebug;
            dsconfig.DebugFilePath = config.DebugFilePath;


            string dbNames = ","+name+",";
            foreach (DBConfigNode node in config.DbConfigs.Values)
            {
                if (dbNames.Contains(","+node.Name+","))
                {
                    dsconfig.DbConfigs.Add(node.Name, node);
                }
            }
            return GetDbProvider(dsconfig,distributed,rw_separate);

        }

        public static IDBBase GetDbProvider(Dictionary<string, DBConfigNode> dbConfigs)
        {
            bool distributed = false;
            if (dbConfigs.Count > 1)
            {
                distributed = true;
            }

            return GetDbProvider(dbConfigs, distributed, true);
        }

        public static IDBBase GetDbProvider(Dictionary<string, DBConfigNode> dbConfigs, bool distributed, bool rw_separate)
        {
            DBScriptConfig config = new DBScriptConfig();
            config.DbConfigs=dbConfigs;
            
            return GetDbProvider(config,distributed,rw_separate);
        }

        public static IDBBase GetDbProvider(DBScriptConfig config, bool distributed, bool rw_separate)
        {
            IDBBase f = null;
            DataBaseType dbType = DataBaseType.MsSQL;
            //Console.WriteLine(distributed);
            foreach (DBConfigNode node in config.DbConfigs.Values)
            {
                dbType = node.DbType;
                break;
            }

            switch (dbType.ToString())
            {
                case "MsSQL":
                    f = new MsSQLProvider(config, distributed, rw_separate);
                    break;
                case "MsSQL2k":
                    f = new MsSQL2kProvider(config, distributed, rw_separate);
                    break;
                case "MySQL":
                    f = new MySQLProvider(config, distributed, rw_separate);
                    break;
                case "SQLite":
                    f = new SQLiteProvider(config, distributed, rw_separate);
                    break;
                default:
                    throw new Exception("没有匹配的适配器！");
                    break;
            }
            return f;
        }
        #endregion

    }
}
